Skip to content

fix: send push notifications as SSE events on the SSE transport - #27258

Merged
Artur- merged 9 commits into
mainfrom
fix/sse-push-critical-notifications
Nov 30, 2026
Merged

Artur- merged 9 commits into
mainfrom
fix/sse-push-critical-notifications

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27189

behavior deviation · flow-server · apps using the experimental SERVER_SENT_EVENTS push transport

With the SSE push transport, a tab that reconnects after its session expired or its UI is gone never got the session-expired notification or the refresh. The notification was written as a bare JSON line, which EventSource ignores, so the client kept reconnecting every few seconds until the next heartbeat (5 minutes by default) or until the browser reported going offline. The notification is now framed as an SSE event. Workaround before this fix: use the WebSocket or long-polling transport.

Risks:

  • ⚠️ Behaviour change: on SSE, a reconnect that is rejected now gets a data: event instead of a bare JSON line. Nothing could read the old reply.
  • ✅ No public API, security, memory, serialization or performance impact. Other transports and already open SSE connections are unchanged.

Context. Atmosphere's SSE interceptor frames a write as an event only on a suspended resource, or when the request carries its CALLBACK_JAVASCRIPT_PROTOCOL attribute, which Atmosphere sets for the handshake of a new connection. The first connection was therefore already framed, but a reconnect carrying an existing tracking id was not.

  • On an SSE resource that is not suspended yet, PushHandler sets CALLBACK_JAVASCRIPT_PROTOCOL on the request before writing the notification, so Atmosphere adds the event framing, padding and message size prefix itself.
  • Only Atmosphere's SSE interceptor reads that attribute, and on a suspended SSE resource it already frames the write, so setting it for other transports or open connections would change nothing. The transport and suspended checks only keep the change to the case it fixes.
  • Added an integration test for each transport (long polling, WebSocket, SSE): the server closes the push connection after the session cookie is gone, and the page must reload from the session-expired notification sent on the new connection. Without the fix, the SSE variant keeps reconnecting and fails; the other two already passed and guard against regressions. The button that closes the push connection is added to the shared BasicPushView. It replaces the unit test added earlier, which only checked the attribute on a mocked resource.

Working on behalf of @mshabarov

Atmosphere frames only writes to a suspended resource, so session
expired, UI not found and refresh notifications reached the client as a
bare JSON line that EventSource ignores.

Fixes #27189
@totally-not-ai
totally-not-ai Bot requested a review from mshabarov November 24, 2026 09:12
@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Bugfix

How to test

  1. Open flow-tests/test-root-context/…/push/BasicPushSseView.java (with the ssePushTransport feature flag on) in one tab.
  2. In a second tab of the same browser, open InvalidateHttpSessionView and click "Invalidate HTTP session".
  3. Go back to the first tab: it reloads (or shows the session-expired notification if enabled) instead of reconnecting forever. You can also run curl -N "http://localhost:8888/VAADIN/push?v-r=push&X-Atmosphere-Transport=sse&X-Atmosphere-tracking-id=0" without a session cookie and see a data:{...} line.

resource.getResponse()
.setContentType(JsonConstants.JSON_CONTENT_TYPE);
resource.getResponse().getWriter().write(notificationJson);
if (resource.transport() == TRANSPORT.SSE

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this location need to take the transport into account if no other location needs to?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Artur- This is the only place that writes straight to the response of a push resource. All other push messages go through the Broadcaster (AtmospherePushConnection.sendMessage) to a suspended resource, and Atmosphere's interceptors add each transport's framing there, including data: for SSE.

Here the resource is usually not suspended yet (the connection is rejected in onConnect or the first receive). On that path:

  • WebSocket and long polling need no framing beyond the length prefix, and TrackMessageSizeInterceptor adds that whether or not the resource is suspended. So the plain JSON write works for them.
  • SSEAtmosphereInterceptor only writes padding on the first write to an unsuspended resource. It skips the data: prefix and the closing blank line, so EventSource drops the message. That's why only SSE needs its own branch, and only while the resource is unsuspended.

The other option would be to suspend the resource first and broadcast the notification, so Atmosphere frames it for every transport. That would change the disconnect flow for all transports to fix one, so I kept the change local. I can switch to that approach if you prefer it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would sound better not to do low level writes but how large is the blast radius?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Artur- Good point. I replaced the low-level write, so the blast radius stays the same as before: only SSE resources that are not suspended yet.

Atmosphere's SSE interceptor already frames writes on an unsuspended resource when the request has the FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL attribute. Atmosphere's JavaScriptProtocol sets it for the handshake of a new connection. So the notification now goes through the normal Atmosphere writer, and PushHandler only sets that attribute first. Atmosphere adds the padding, the data: framing and the message size prefix itself.

I checked the wire output on embedded Jetty with real Atmosphere, using a push request without a session:

  • Reconnect with an existing tracking id: before 45|{...sessionExpired...} (unframed), after data:45|{...} followed by a blank line.
  • First connection handshake (tracking id 0): data:45|{...} both before and after, because Atmosphere already set the attribute there.

So the bug only hit reconnects, which is the idle-tab case. WebSocket, long polling and already open SSE connections are untouched. A test now checks that WebSocket requests don't get the attribute.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 1 709 files  ±0   1 799 suites  ±0   2h 10m 59s ⏱️ - 1m 50s
13 917 tests ±0  13 845 ✅ ±0  72 💤 ±0  0 ❌ ±0 
14 595 runs  ±0  14 523 ✅ ±0  72 💤 ±0  0 ❌ ±0 

Results for commit 8850e91. ± Comparison against base commit 754bd95.

♻️ This comment has been updated with latest results.

Instead of writing the event to the container response, mark the
request the way Atmosphere marks a connection handshake, so its SSE
interceptor adds the framing, padding and length prefix itself.
@Artur-
Artur- marked this pull request as ready for review November 24, 2026 13:34

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — left 2 comments.

Finding
⚠️ The notification response still declares application/json, which a browser EventSource rejects regardless of the new framing
🧪 The new test asserts that a request attribute was set on a mock, not that the notification is framed as an SSE event

FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL,
Boolean.TRUE);
}
resource.getResponse()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ The notification response still declares application/json, which a browser EventSource rejects regardless of the new framing

This path never suspends the resource, and the write sets JsonConstants.JSON_CONTENT_TYPE on the response. The reconnect is a fresh HTTP request whose headers are not committed yet, so the SSE response goes out as application/json; charset=UTF-8. Per the HTML spec, EventSource fails the connection when the content type is not text/event-stream, and the message body is never delivered to the client — the framing fix would then change the wire bytes without fixing the reported symptom.

The establish path sets text/plain the same way and SSE push does work, which implies Atmosphere re-asserts text/event-stream somewhere — most plausibly around suspend, which this path never reaches. Worth confirming in a real browser (check the response headers, not just the body) rather than with a raw HTTP client.

PushHandler.java:671 · correctness

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response goes out as text/event-stream. On a resource that isn't suspended, the SSE interceptor's first write goes through writePadding, which calls response.setContentType("text/event-stream") and flushes the padding. That happens before any payload, so the JSON content type set here is replaced while the headers are still uncommitted. The curl output in the issue already shows Content-Type: text/event-stream;charset=utf-8 for this path.

I also checked it in a real browser. The new SsePushSessionExpiredIT drops the session cookie and closes the SSE connection from the server, then waits for the page to reload from the session-expired notification on the new connection. EventSource accepts the response and the page reloads. Without the fix, the page never reloads and the test fails.

verify(request, never()).setAttribute(
eq(FrameworkConfig.CALLBACK_JAVASCRIPT_PROTOCOL), any());
} else {
inOrder.verify(request).setAttribute(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 The new test asserts that a request attribute was set on a mock, not that the notification is framed as an SSE event

CONVENTIONS.md (Testing): "Assert the observable outcome — DOM state, response, emitted event, public API result — not how it is produced. A test must fail when the behavior breaks and keep passing through a refactor that preserves it."

The resource is a Mockito mock, so no Atmosphere interceptor runs and nothing about the actual output is checked. The test passes whether or not Atmosphere still frames an unsuspended SSE write on this attribute, and it would fail for the alternative implementation discussed in the review (suspend and broadcast) even though that produces the same wire output. The only evidence that the fix works is the manual wire check in the PR discussion.

There are IdlePushChannel*IT variants for long polling and websocket but none for SSE, and none cover session expiry, so nothing in CI would catch a regression here.

PushHandlerTest.java:733 · tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I replaced the mock-based test with SsePushSessionExpiredIT, which runs in the browser and checks the outcome: after the session is gone, the SSE reconnect must deliver the session-expired notification and the page must reload. It fails without the fix and passes with it. The IT runs in the Slow Tests job together with the other PushTests.

I also removed the WebSocket assertion added earlier in this PR, since it only checked the attribute too. The transport and suspended checks don't change the output on their own: only the SSE interceptor reads the attribute, and on a suspended SSE resource it already frames the write. So a test pinning those checks would be testing the implementation, not the behaviour. The PR description now says this.

Replace the unit test that only checked a request attribute on a mocked
resource with an integration test: the server closes the SSE push
connection after the session cookie is gone, and the page must reload
from the session expired notification sent on the new connection. The
test fails without the fix, as the client then keeps reconnecting.

The websocket assertion added earlier in this PR is removed with it, as
it also checked the attribute rather than the output.
*/
@CustomPush(transport = Transport.SERVER_SENT_EVENTS)
@Route(value = "com.vaadin.flow.uitest.ui.push.SsePushReconnectView", layout = ViewTestLayout.class)
public class SsePushReconnectView extends BasicPushSseView {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a similar test already for long polling and websocket that should be extended somehow, or are they done separately like this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No existing test covered this case for long polling or WebSocket. The closest were IdlePushChannelIT and BasicPushIT, but neither drops the session before the push connection reopens. So I followed the IdlePushChannelIT pattern: PushSessionExpiredIT is now abstract, with PushSessionExpiredLongPollingIT, PushSessionExpiredWebsocketIT and PushSessionExpiredSseIT subclasses using the existing BasicPush*Views. The button that closes the push connection from the server moved into BasicPushView, and the separate SSE-only view is gone. All three pass with the fix. Without it, only the SSE one fails.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this require some update to the test migration doc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Artur- This view is gone: its button now lives in the shared BasicPushView, and the test runs once per transport as PushSessionExpired{LongPolling,Sse,Websocket}IT.

MIGRATION.md needs no change. Transport is picked per test with @CustomPush, so this adds no new module permutation for the ledger.

MIGRATION_LIST.md did need an update, and I've made it. The four new ITs are now listed under push → test-default, next to the other push ITs in test-root-context, and the counts are updated. I added them by hand instead of rerunning the generator. Rerunning it would also pick up ITs that other PRs added but never listed (DeclaredJsImportIT, StringFallbackIT, QueuedHistoryUpdateIT, DevBundleJsDefinitionImportIT), and those don't belong in this PR.

totally-not-ai Bot and others added 2 commits November 27, 2026 12:56
Follow the IdlePushChannelIT pattern: an abstract test with one
subclass per transport, so long polling and websocket are covered too.
The button that closes the push connection from the server moves into
BasicPushView, which all transport views extend, replacing the
SSE-only SsePushReconnectView.
The new PushSessionExpired*IT classes live in test-root-context next to
the other push ITs, so they move to test-default with the push package.

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Nothing to flag — the changes look good.

@sonarqubecloud

Copy link
Copy Markdown

@Artur-
Artur- added this pull request to the merge queue Nov 30, 2026
Merged via the queue into main with commit e4e8db2 Nov 30, 2026
66 checks passed
@Artur-
Artur- deleted the fix/sse-push-critical-notifications branch November 30, 2026 08:36
@github-actions

Copy link
Copy Markdown
Contributor

Documentation Bot: No documentation needed for this change.

This is a bug fix (BEHAVIOR_CHANGE) to PushHandler: on the experimental SERVER_SENT_EVENTS push transport, a reconnecting tab did not get the session-expired notification because it was written as a bare JSON line instead of a framed SSE event, so EventSource ignored it. The documentation already describes the general behavior that a Session expired notification is sent to the browser and handled by the client engine (application-lifecycle.adoc); this fix only makes the SSE transport actually deliver that notification, matching what the WebSocket and long-polling transports already did and what the docs already promise.

Nothing was opened in vaadin/docs. If you disagree, the change has to be documented by hand there.

Generated by Documentation Bot for #27258 · agent · 38.4 AIC · ⊞ 11.7K · ◷

vaadin-bot pushed a commit that referenced this pull request Nov 30, 2026
…) (CP: 25.4)

Fixes #27189

**behavior deviation** · flow-server · apps using the experimental
`SERVER_SENT_EVENTS` push transport

With the SSE push transport, a tab that reconnects after its session
expired or its UI is gone never got the session-expired notification or
the refresh. The notification was written as a bare JSON line, which
`EventSource` ignores, so the client kept reconnecting every few seconds
until the next heartbeat (5 minutes by default) or until the browser
reported going offline. The notification is now framed as an SSE event.
Workaround before this fix: use the WebSocket or long-polling transport.

**Risks:**
- ⚠️ Behaviour change: on SSE, a reconnect that is rejected now gets a
`data:` event instead of a bare JSON line. Nothing could read the old
reply.
- ✅ No public API, security, memory, serialization or performance
impact. Other transports and already open SSE connections are unchanged.

**Context.** Atmosphere's SSE interceptor frames a write as an event
only on a suspended resource, or when the request carries its
`CALLBACK_JAVASCRIPT_PROTOCOL` attribute, which Atmosphere sets for the
handshake of a new connection. The first connection was therefore
already framed, but a reconnect carrying an existing tracking id was
not.

- On an SSE resource that is not suspended yet, `PushHandler` sets
`CALLBACK_JAVASCRIPT_PROTOCOL` on the request before writing the
notification, so Atmosphere adds the event framing, padding and message
size prefix itself.
- Only Atmosphere's SSE interceptor reads that attribute, and on a
suspended SSE resource it already frames the write, so setting it for
other transports or open connections would change nothing. The transport
and suspended checks only keep the change to the case it fixes.
- Added an integration test for each transport (long polling, WebSocket,
SSE): the server closes the push connection after the session cookie is
gone, and the page must reload from the session-expired notification
sent on the new connection. Without the fix, the SSE variant keeps
reconnecting and fails; the other two already passed and guard against
regressions. The button that closes the push connection is added to the
shared `BasicPushView`. It replaces the unit test added earlier, which
only checked the attribute on a mocked resource.

Working on behalf of `@mshabarov`

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Co-authored-by: Artur Signell <artur@vaadin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hackathon 25.4: Critical push notifications are not SSE-framed on the server-sent events transport

3 participants